package com.xps.and.yuntong.Ui;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.xps.and.yuntong.App;
import com.xps.and.yuntong.Data.bean1.AlipayBean;
import com.xps.and.yuntong.Data.net.UserNetWorks;
import com.xps.and.yuntong.R;
import com.xps.and.yuntong.Utils.APay_v2;
import com.xps.and.yuntong.Utils.GlobalPositionInfo;
import com.xps.and.yuntong.base.BaseActivity;

import java.util.Date;

import rx.Subscriber;

/**
 * Created by administrator on 2017-12-15.
 */

public class ChongzhiActivity extends BaseActivity implements View.OnClickListener {
    private ImageView iv_chongzhi_fanhui, iv_zhifubao_state, iv_weixin_state, iv_right;
    private TextView tv_middle, tv_qian, tv_jine1, tv_jine2, tv_jine3, tv_jine4;
    private EditText et_jine;
    private LinearLayout ll_queren_zhifu;
    private int state = 0;
    String zfbID;

    @Override
    protected int getLayout() {
        return R.layout.activity_chongzhi;
    }

    @Override
    protected void setData() {
    }

    @Override
    protected void init(Bundle inBundle) {
        initView();
    }

    private void initView() {
        iv_chongzhi_fanhui = (ImageView) findViewById(R.id.iv_fanhui);
        iv_zhifubao_state = (ImageView) findViewById(R.id.iv_zhifubao_state);
        iv_weixin_state = (ImageView) findViewById(R.id.iv_weixin_state);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        iv_right.setVisibility(View.GONE);
        tv_middle = (TextView) findViewById(R.id.tv_middle);
        tv_middle.setText(R.string.chong_zhi1);
        tv_qian = (TextView) findViewById(R.id.tv_qian);
        tv_jine1 = (TextView) findViewById(R.id.tv_jine1);
        tv_jine2 = (TextView) findViewById(R.id.tv_jine2);
        tv_jine3 = (TextView) findViewById(R.id.tv_jine3);
        tv_jine4 = (TextView) findViewById(R.id.tv_jine4);
        et_jine = (EditText) findViewById(R.id.et_jine);
        ll_queren_zhifu = (LinearLayout) findViewById(R.id.ll_queren_zhifu);
        iv_chongzhi_fanhui.setOnClickListener(this);
        iv_zhifubao_state.setOnClickListener(this);
        iv_weixin_state.setOnClickListener(this);
        ll_queren_zhifu.setOnClickListener(this);
        tv_jine1.setOnClickListener(this);
        tv_jine2.setOnClickListener(this);
        tv_jine3.setOnClickListener(this);
        tv_jine4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_fanhui:
                finish();
                break;
            case R.id.tv_jine1:
                tv_jine1.setBackgroundResource(R.drawable.xian_kuang_lv);
                tv_jine2.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine3.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine4.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine1.setTextColor(Color.parseColor("#2ebf12"));
                tv_jine2.setTextColor(Color.parseColor("#8a8a8a"));
                tv_jine3.setTextColor(Color.parseColor("#8a8a8a"));
                tv_jine4.setTextColor(Color.parseColor("#8a8a8a"));
                break;
            case R.id.tv_jine2:
                tv_jine1.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine2.setBackgroundResource(R.drawable.xian_kuang_lv);
                tv_jine3.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine4.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine1.setTextColor(Color.parseColor("#8a8a8a"));
                tv_jine2.setTextColor(Color.parseColor("#2ebf12"));
                tv_jine3.setTextColor(Color.parseColor("#8a8a8a"));
                tv_jine4.setTextColor(Color.parseColor("#8a8a8a"));
                break;
            case R.id.tv_jine3:
                tv_jine1.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine2.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine3.setBackgroundResource(R.drawable.xian_kuang_lv);
                tv_jine4.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine1.setTextColor(Color.parseColor("#8a8a8a"));
                tv_jine2.setTextColor(Color.parseColor("#8a8a8a"));
                tv_jine3.setTextColor(Color.parseColor("#2ebf12"));
                tv_jine4.setTextColor(Color.parseColor("#8a8a8a"));
                break;
            case R.id.tv_jine4:
                tv_jine1.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine2.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine3.setBackgroundResource(R.drawable.xian_kuang);
                tv_jine4.setBackgroundResource(R.drawable.xian_kuang_lv);
                tv_jine1.setTextColor(Color.parseColor("#8a8a8a"));
                tv_jine2.setTextColor(Color.parseColor("#8a8a8a"));
                tv_jine3.setTextColor(Color.parseColor("#8a8a8a"));
                tv_jine4.setTextColor(Color.parseColor("#2ebf12"));
                break;
            case R.id.iv_zhifubao_state:
                state = 0;
                iv_zhifubao_state.setBackgroundResource(R.drawable.zhifu_yixuan);
                iv_weixin_state.setBackgroundResource(R.drawable.zhifu_weixuan);
                break;
            case R.id.iv_weixin_state:
                state = 1;
                iv_zhifubao_state.setBackgroundResource(R.drawable.zhifu_weixuan);
                iv_weixin_state.setBackgroundResource(R.drawable.zhifu_yixuan);
                break;
            case R.id.ll_queren_zhifu:
                if (checkAliPayInstalled(ChongzhiActivity.this)) {
                    if (et_jine.getText().toString().equals("")) {
                        JUtils.Toast("请输入金额");
                    } else {
                        getAlipay();
                    }
                } else {
                    JUtils.Toast("请安装支付宝客户端");
                }
                break;
        }
    }

    private boolean checkAliPayInstalled(Context context) {
        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }

    private void anniuState() {
        if (state == 0) {
            iv_zhifubao_state.setBackgroundResource(R.drawable.zhifu_yixuan);
            iv_zhifubao_state.setClickable(false);
            iv_weixin_state.setBackgroundResource(R.drawable.zhifu_weixuan);
            iv_weixin_state.setClickable(true);
        }
        if (state == 1) {
            iv_zhifubao_state.setBackgroundResource(R.drawable.zhifu_weixuan);
            iv_zhifubao_state.setClickable(true);
            iv_weixin_state.setBackgroundResource(R.drawable.zhifu_yixuan);
            iv_weixin_state.setClickable(false);
        }
    }

    public void getAlipay() {
        UserNetWorks.getAlipay(et_jine.getText().toString(), new Subscriber<AlipayBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(AlipayBean alipayBean) {
                if (alipayBean.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(alipayBean.getReturn_msg());
                } else if (alipayBean.getReturn_code().equals("SUCCESS")) {
                    zfbID = alipayBean.getReturn_body().getAliResp();
                    APay_v2.getInstance().setContext(ChongzhiActivity.this).doPay_v2(zfbID);
                }
            }
        });
    }
}
